"""
find() find-all() select()
1查找标签
2查找标签的文本内容
3 查找某标签的属性值
4 查找某标签内的标签
"""

import requests
from bs4 import BeautifulSoup

# get请求：url，包含user-agent的请求头
url = 'https://www.baidu.com/'
h = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'}
r = requests.get(url, headers=h)
html = r.content.decode()
soup = BeautifulSoup(html, 'lxml')

# 查询标签：查找div标签，且该标签的类名为：title-text c-font-medium c-color-t
r1 = soup.find('div', {'class': 'title-text c-font-medium c-color-t'})
print(r1)

# 查找标签的文本内容：text string
r2 = soup.find('div', {'class': 'title-text c-font-medium c-color-t'}).string
print(r2)

# 查找某标签的属性值
# 获取百度热榜的6条文本内容和详细url
r3 = soup.find_all('li', {'class': 'hotsearch-item'})
list_data = []
for e in r3:
    name = e.select('span')[1].string
    url = e.find('a')['href']
    data = {'标题': name, '详情链接': url}
    list_data.append(data)
#将列表写入文件
with open('baidu.txt','w',encoding='utf-8') as f:
    # 文件写入，参数是str
    f.write(list_data.__str__())
